#define MAIR_VALUE 0xbbff440c0400
#define TCR_VALUE  0x480100010

.globl memzero
memzero:
	str xzr, [x0], #8
	subs x1, x1, #8
	b.gt memzero
	ret

.global init_page
init_page:
	ldr x0, =KERNEL_START
	mvn x4, x0
	/* put &pud to pgd_t */
	ldr x0, =init_pgd
	and x0, x0, x4
	ldr x1, =init_pud
	and x1, x1, x4
	orr x1, x1, #0x3
	str x1, [x0]

	/* put &pmd to pud_t */
	ldr x0, =init_pud
	and x0, x0, x4
	ldr x1, =init_pmd
	and x1, x1, x4
	orr x1, x1, #0x3
	str x1, [x0]

	/* mapping 8 sections (8x2MB) in pmd */
	ldr  x0, =init_pmd
	and  x0, x0, x4
	mov  x1, #0x00
	movz x3, #0x0711, lsl #0
	movk x3, #0x0000, lsl #16
	movk x3, #0x0000, lsl #32
	movk x3, #0x00c8, lsl #48
	orr  x1, x1, x3
	mov  x2, #8
	mov  x3, #0x200000
loop:
	str x1, [x0], #8
	subs x2, x2, #1
	add x1, x1, x3
	b.ne loop
	ret

.global enable_mmu
enable_mmu:
	tlbi vmalle1
	dsb  nsh
	movz x0, #0x30, lsl #16
	msr  cpacr_el1, x0
	mov  x0, #0x1000
	msr  mdscr_el1, x0
	ldr  x0, =MAIR_VALUE
	msr  mair_el1, x0
	ldr  x0, =TCR_VALUE
	msr  tcr_el1, x0

	ldr  x0, =KERNEL_START
	mvn  x4, x0

	ldr  x0, =init_pgd
	and  x0, x0, x4
	msr  ttbr1_el1, x0
	ldr  x0, =init_pgd
	and  x0, x0, x4
	msr  ttbr0_el1, x0
	isb

	ldr  x1, =mmu_enabled
	mov  x0, #0x01

	tlbi vmalle1
	dsb  nsh
	isb

	msr  sctlr_el1, x0

	isb
	ic   iallu
	dsb  nsh
	isb
	br   x1


.section .bss
.align  12
.global init_pgd
init_pgd:
	.skip 4096
.global init_pud
init_pud:
	.skip 4096
.global init_pmd
init_pmd:
	.skip 4096
